package com.yiwei.zxp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.bean.CustomParamsBean;
import com.yiwei.zxp.api.bean.ParamsBean;
import com.yiwei.zxp.util.UI;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class CustomParamsAdapter extends ItemAdapter<ParamsBean, CustomParamsAdapter.DefaultHolder> {


    public CustomParamsAdapter(Context context) {
        super(context);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_custom_params, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        holder.setIsRecyclable(false);
        ParamsBean item = getItem(position);


        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onDelete(position);
                }
            }
        });


        holder.editTitle.setText(item.name);
        holder.editContent.setText(item.value);

        holder.editTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                getmSource().get(position).name = UI.toString(holder.editTitle);
            }
        });

        holder.editContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                getmSource().get(position).value = UI.toString(holder.editContent);

            }
        });


    }

    class DefaultHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.edit_title)
        EditText editTitle;
        @BindView(R.id.edit_content)
        EditText editContent;
        @BindView(R.id.btn_delete)
        ImageView btnDelete;


        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onDelete(int poi);

    }
}
